<?php 
    require_once ("Includes/simplecms-config.php"); 
    require_once  ("Includes/connectDB.php");
    require_once ("Functions/printscores.php");
    require_once ("Functions/database.php");
    include("Includes/header.php");
    define ("NUM_COLS", 10);
?>


    <div id="main">
    <h3>Swimmers:</h3>
        <?php
            $statement = $databaseConnection->prepare("SELECT id, nickname FROM swimmers WHERE team_id = 1 ORDER BY nickname");
            $statement->execute();
            if($statement->error)
                die("Database query failed: " . $statement->error);
            $statement->store_result();
            if (($num = $statement->num_rows) == NULL)
                die("<div class=\"error\">Error: No swimmers found</div>");
            $statement->bind_result($id, $nickname);
            
            $row_per_col =  intval($num / NUM_COLS); 
            $row = 0;
            $col = 0;
            // create arrays
            $results = array();
            while($statement->fetch()) {
                $results[strval($row).'_'.strval($col).'_id'] = $id;
                $results[strval($row).'_'.strval($col).'_nick'] = $nickname;

                $row++;
                if ($row == $row_per_col){
                    $col++;
                    $row = 0;
                }
            }
            
            echo "<table width=\"100%\">\n";
            for($i = 0; $i < $row_per_col; $i++)
            {
                echo "<tr>";
                for ($j = 0; $j < NUM_COLS; $j++)
                    echo '<td><a href="swimmer.php?swimid='.$results[strval($i).'_'.strval($j).'_id'].
                         '">'.$results[strval($i).'_'.strval($j).'_nick'].'</a></td>';
                echo "</tr>\n";
            }
            echo "</table>\n";
        ?>
    </div>

</div> <!-- End of outer-wrapper which opens in header.php -->

<?php 
    include ("Includes/footer.php");
 ?>